//
//  DKWHeaderTableViewCell.m
//  CreditHub
//
//  Created by chenglang on 2024/9/15.
//

#import "DKWHeaderTableViewCell.h"
#import "LoadDetailsViewController.h"
#import "HomeRequest.h"
#import "WebViewController.h"
#import "UUID.h"

@implementation DKWHeaderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)showDicInfo:(NSDictionary *)dicInfo{
    if(dicInfo){
        self.dicInfo = dicInfo;
        self.labelPrice.text = CHString(dicInfo[@"last"]);
        self.labelLoan.text = CHString(dicInfo[@"comfort"]);
        self.labelDay.text = CHString(dicInfo[@"best"]);
        self.labelRateTitle.text = CHString(dicInfo[@"thegarden"]);
        self.labelRate.text = CHString(dicInfo[@"swiftly"]);
        [self.buttonApply setTitle:CHString(dicInfo[@"comfort"]) forState:UIControlStateNormal];
        self.labelTitle.text = CHString(dicInfo[@"sobbingtinker"]);
    }
}

- (IBAction)clickApply:(id)sender {
    if( [UserInfoModel isLogin] ){
        __weak typeof(self) weakSelf = self;
        xhl_hudLoading();
        [HomeRequest requestHomeCanpinLightat:self.dicInfo[@"glad"] success:^(NSURLSessionDataTask *task, id response) {
            
            if( [response[@"ofexcitement"] integerValue] == 0 ){
                xhl_hudDismiss();
                NSString *spindly = CHString(response[@"pity"][@"spindly"]);
                if(spindly.intValue == 1){
                    NSString *faint = CHString(response[@"pity"][@"faint"]);
                    if ([faint length] >= 4) {
                        NSString *substring = [faint substringToIndex:4];
                        if ([substring isEqualToString:@"http"]) {
                            WebViewController *webView = [[WebViewController alloc] init];
                            webView.url = [weakSelf getRequestUrlString:faint] ;
                            [[UIView xhl_getTopViewController].navigationController pushViewController:webView animated:YES];
                        } else {
                            
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:faint] options:@{} completionHandler:^(BOOL success) {
                                
                            }];
                            
                        }
                    }
                    
                }else{
                    LoadDetailsViewController *vc = [[LoadDetailsViewController alloc] init];
                    vc.detailId = weakSelf.dicInfo[@"glad"];
                    [[UIView xhl_getTopViewController].navigationController pushViewController:vc animated:YES];
                }
            }else{
                xhl_showMessage(response[@"shared"]);
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            xhl_showMessage(error.localizedDescription);
        }];
       
    }else{
        [UserInfoModel login];
    }
    
}
- (NSString *)getRequestUrlString:(NSString *)str {
    
    NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    
    NSString *url= [NSString stringWithFormat:@"%@&areal=%@&boo=%@&howling=%@&ear=%@&knew=%@&minute=%@&which=1",str,[UserInfoModel getUserInfo].areal,appVersion,[BaseService friendlyDeviceName],[UUID getUUID],[[UIDevice currentDevice] systemVersion],[BaseService getIDFA]];
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
    return url;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
